package it.uniroma2.exp.tools;

import it.uniroma2.dtk.dt.GenericDT;
import it.uniroma2.util.tree.Tree;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class SRLDataPrep {

	/**
	 * @param args
	 * term vectors file
	 * terms file
	 * type vectors file
	 * vector size
	 * base srl data files folder
	 * base output folder
	 * type of CDS class to use
	 */
	public static void main(String[] args) {
		GenericDT dt = null;
		File baseFolder = null;
		File outputFolder = null;
		try {
			dt = new GenericDT(0, Integer.parseInt(args[3]), true, true, Integer.parseInt(args[6]));
			baseFolder = new File(args[4]);
			outputFolder = new File(args[5]+File.separator+dt.getType()+File.separator+args[3]);
			if (!outputFolder.exists())
				outputFolder.mkdirs();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			for (File file : baseFolder.listFiles()) {
				System.out.println("Computing "+file.getName()+"...");
				BufferedReader in = new BufferedReader(new FileReader(file));
				BufferedWriter out = new BufferedWriter(new FileWriter(new File(outputFolder, file.getName()+".svm")));
				String line = in.readLine();
				while (line != null) {
					String tree = line.substring(line.indexOf("|BT|")+4, line.indexOf("|BV|")).trim(); 
					out.write(line.substring(0, line.indexOf("|BV|")) + "|ET| " + vectorToSvmString(dt.dt(Tree.fromPennTree(tree)))+" |EV|");
					out.newLine();
					line = in.readLine();
				}
				out.close();
				in.close();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static String vectorToSvmString(double[] vector) {
		String result = "";
		for (int i=0; i<vector.length; i++)
			if (vector[i] != 0)
				result += (i+1)+":"+vector[i]+" ";
		return result.trim();
	}
}
